package top.honlnk.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.honlnk.common.constant.AdminConstant;
import top.honlnk.common.constant.AutoLoginOptionConstant;
import top.honlnk.common.constant.StaffConstant;
import top.honlnk.common.constant.UserConstant;
import top.honlnk.common.exception.CustomException;
import top.honlnk.common.result.Result;
import top.honlnk.pojo.dto.AdminUpdateRotaDTO;
import top.honlnk.pojo.dto.ServiceStaffLoginDTO;
import top.honlnk.pojo.dto.ServiceStaffLoginPowerDTO;
import top.honlnk.pojo.dto.UserUpdateTableDTO;
import top.honlnk.pojo.entity.OcrAdminInfo;
import top.honlnk.pojo.vo.ServiceStaffLoginPowerVO;
import top.honlnk.pojo.vo.StaffGetRotaVO;
import top.honlnk.pojo.vo.ServiceStaffLoginVO;
import top.honlnk.pojo.vo.StaffGetTableVO;
import top.honlnk.service.AdminService;
import top.honlnk.service.ServiceStaffService;
import top.honlnk.service.UserService;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/service_staff")
public class serviceStaffController {

    @Autowired
    private ServiceStaffService serviceStaffService;

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result<ServiceStaffLoginVO> login(@RequestBody ServiceStaffLoginDTO serviceStaffLoginDTO) {
        log.info("用户登录，参数为: {}", serviceStaffLoginDTO);
        if (serviceStaffLoginDTO.getAccount() == null || serviceStaffLoginDTO.getAccount().isEmpty()) {
            throw new CustomException("账号不能为空");
        }
        if (serviceStaffLoginDTO.getPassword() == null || serviceStaffLoginDTO.getPassword().isEmpty()) {
            throw new CustomException("密码不能为空");
        }
        if (!AutoLoginOptionConstant.OPTIONS.contains(serviceStaffLoginDTO.getAutoLoginOption())) {
            throw new CustomException("自动登录选项无效");
        }
        ServiceStaffLoginVO loginVO = serviceStaffService.login(serviceStaffLoginDTO);
        return Result.success(loginVO);
    }

    @PostMapping("/login_power")
    public Result<ServiceStaffLoginPowerVO> loginPower(@RequestBody ServiceStaffLoginPowerDTO serviceStaffLoginPowerDTO) {
        log.info("登录权限，参数为: {}", serviceStaffLoginPowerDTO);
        if (serviceStaffLoginPowerDTO.getAccount() == null || serviceStaffLoginPowerDTO.getAccount().isEmpty()){
            throw new CustomException("账号不能为空");
        }
        if (serviceStaffLoginPowerDTO.getPasswordPower() == null || serviceStaffLoginPowerDTO.getPasswordPower().isEmpty()) {
            throw new CustomException("密码不能为空");
        }
        ServiceStaffLoginPowerVO loginPowerVO = serviceStaffService.loginPower(serviceStaffLoginPowerDTO);
        return Result.success(loginPowerVO);
    }

    @GetMapping("/get_all_rota")
    public Result<List<StaffGetRotaVO>> getAllRota() {
        return Result.success(serviceStaffService.getAllRota());
    }

    @GetMapping("/get_all_table")
    public Result<List<StaffGetTableVO>> getAllTable() {
        return Result.success(serviceStaffService.getTable(StaffConstant.GET_ALL_TABLE));
    }

    @GetMapping("/get_table_by_key")
    public Result<List<StaffGetTableVO>> getTableByKey(HttpServletRequest request) {
        String key = request.getHeader("key");
        return Result.success(serviceStaffService.getTable(key));
    }

    @GetMapping("/get_ocr_admin_info")
    public Result<List<OcrAdminInfo>> getOcrAdminInfo() {
        return Result.success(serviceStaffService.getOcrAdminInfo());
    }



    @PutMapping("/update_table")
    public Result<String> updateTable(@RequestBody UserUpdateTableDTO userUpdateTableDTO, HttpServletRequest request) {
        String submitId = request.getHeader("submitId");
        if (userUpdateTableDTO.getTable() == null)
            throw new CustomException(UserConstant.TABLE_IS_NULL);
        userService.setTable(userUpdateTableDTO, submitId);
        return Result.success();
    }

    @PutMapping("/update_rota")
    public Result<String> updateRota(@RequestBody AdminUpdateRotaDTO adminUpdateRotaDTO, HttpServletRequest request) {
        log.info("更新排班表，参数为: {}", Arrays.deepToString(adminUpdateRotaDTO.getRota()));
        if (adminUpdateRotaDTO.getRota() == null)
            throw new CustomException(AdminConstant.ROTA_IS_NULL);
        adminService.updateRota(adminUpdateRotaDTO, request.getHeader("key"));
        return Result.success();
    }

    @PutMapping("/update_ocr_info")
    public Result<String> updateOcrInfo(@RequestBody OcrAdminInfo ocrAdminInfo) {
        serviceStaffService.setOcrAdminInfo(ocrAdminInfo);
        return Result.success();
    }

    @PostMapping("/add_ocr_info")
    public Result<String> addOcrInfo(@RequestBody OcrAdminInfo ocrAdminInfo) {
        serviceStaffService.addOcrAdminInfo(ocrAdminInfo);
        return Result.success();
    }

    @GetMapping("/refresh_access_token")
    public Result<String> refreshAccessToken(int id) {
        serviceStaffService.refreshAccessToken(id);
        return Result.success();
    }

    @DeleteMapping("/del_rota")
    public Result<String> delRota(HttpServletRequest request) {
        String key = request.getHeader("key");
        String success = serviceStaffService.delRota(key);
        return Result.success(success);
    }

    @DeleteMapping("/del_table")
    public Result<String> delTable(HttpServletRequest request) {
        String submitId = request.getHeader("submitId");
        serviceStaffService.delTable(submitId);
        return Result.success();
    }

    @DeleteMapping("/del_ocr_info")
    public Result<String> delOcrInfo(int id) {
        serviceStaffService.delOcrAdminInfo(id);
        return Result.success();
    }
}
